package com.hsy.crm.web.service.asyn;

import com.hsy.crm.web.domain.entity.Data;
import com.hsy.crm.web.domain.entity.Message;
import com.hsy.crm.web.mapper.DataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.LinkedList;

@Service
public class AsynService {

    private static LinkedList<Message> FIFO = new LinkedList<>();

    //Logger logger = LoggerFactory.getLogger(AsynService.class);

    @Autowired
    MarketProfitService marketProfitService;

    @Autowired
    DataMapper dataMapper;

    @Scheduled(fixedDelay = 3000)
    public void calc(){
        try {
            Message message = FIFO.poll();
            if( message != null ){
                marketProfitService.calc(message);

                //for test ,delete
                Data data = new Data();
                data.setId(10000l);
                data.setValue(1);
                dataMapper.updateByPrimaryKeySelective(data);
                //FileUtils.writeStringToFile(new File("D:\\wuwen\\project\\crm-cloud\\lock.ww"),"1");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void addMessage(Message message){
        FIFO.add(message);
    }
}
